package dem1;

import java.util.Arrays;
import java.util.Stack;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-05-07
 * Time: 10:44
 */
public class Test3 {

    public static void shellSort(int[] array){
        int length = array.length;
        while (length > 1){
            length = length / 2;
            shell(array, length);
        }
    }

    public static void shell(int[] array, int gap){
        for(int i = gap; i < array.length; i++){
            int j = i - gap;
            int tmp = array[i];
            for (; j >= 0; j -= gap){
                if (array[j] > tmp){
                    array[j + gap] = array[j];
                }else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    public static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static int find(int[] array, int begin, int end){
        int i = begin;
        while (begin < end){
            while (begin < end && array[end] >= array[i]){
                end--;
            }
            while (begin < end && array[begin] <= array[i]){
                begin++;
            }
            swap(array,end,begin);
        }
        swap(array,begin,i);
        return begin;
    }

    public static void main(String[] args) {
//        int[] arr = { 'F','B','J','G','E','A','I','D','C','H'};
////        //shellSort(arr);
//        shell(arr,5);
//        for (int i = 0; i < arr.length; i++) {
//            System.out.print((char) arr[i] + " ");
//        }

        //int[] arr = {5,4,3,2,1};
        //shellSort(arr);
        //int[] arr = {4,2,8,4,1,0,9,2};
        //shell(arr,1);
        //System.out.println(Arrays.toString(arr));

        int[] arr = {28, 16, 32, 12, 60, 2, 5, 72};
        find(arr,0,arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }
}
